//
//  XS_KCBiaoqian.swift
//  XueDao
//
//  Created by 韩云智 on 2021/1/1.
//

import UIKit
import RxCocoa
import RxSwift

class XS_KCBiaoqian: UIView {
    lazy var disposeBag = DisposeBag()
    var getViewBlock: ((Int)->UIView?)?
    lazy var bottomDic: [String:UIView] = [:]
    init(_ texts: [String], block: ((Int)->UIView?)?) {
        super.init(frame: CGRect(x: 0, y: 0, width: .screenWidth, height: 0))
        self.getViewBlock = block
        upView(texts)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView(_ texts: [String]) {
        guard texts.count > 0 else { return }
        let topView = UIView()
        topView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        addSubview(topView)
        topView.snp.makeConstraints({
            $0.top.left.right.equalToSuperview()
            $0.height.equalTo(.scale*44)
        })
        
        var btns: [UIButton] = []
        var last: UIView?
        for (index, text) in texts.enumerated() {
            let btn = getBtn(text)
            btns.append(btn)
            topView.addSubview(btn)
            btn.snp.makeConstraints({
                $0.top.bottom.equalToSuperview()
                if let last = last {
                    $0.left.equalTo(last.snp.right)
                    $0.width.equalTo(last.snp.width)
                } else {
                    $0.left.equalToSuperview()
                }
                if index == texts.count - 1 {
                    $0.right.equalToSuperview()
                }
            })
            last = btn
        }
        topView.layoutIfNeeded()

        let line = UIView()
        line.layer.cornerRadius = .scale*2
        line.layer.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        line.frame = CGRect(x: 0, y: 40, width: 33, height: 4) * .scale
        topView.addSubview(line)
        
        let bottomView = UIView()
        addSubview(bottomView)
        bottomView.snp.makeConstraints({
            $0.left.right.equalToSuperview()
            $0.top.equalTo(topView.snp.bottom)
            $0.bottom.lessThanOrEqualToSuperview()
        })
        
        let observables = btns.map { button in button.rx.tap.map { button } }
        let selected = Observable.from(observables).merge().startWith(btns[0])
        for (index, button) in btns.enumerated() {
            selected.map ({ [weak self] item in
                if item == button {
                    line.center.x = button.center.x
                    bottomView.removeSubviews()
                    if let view = self?.getBottom(index) {
                        bottomView.addSubview(view)
                        view.snp.makeConstraints({
                            $0.left.top.right.bottom.equalToSuperview()
                        })
                    }
                    return false
                } else {
                    return true
                }
            }).bind(to: button.rx.isEnabled)
            .disposed(by: disposeBag)
        }
    }
    
    func getBottom(_ index: Int) -> UIView? {
        if let view = bottomDic["\(index)"] {
            return view
        }
        if let view = getViewBlock?(index) {
            bottomDic["\(index)"] = view
            return view
        }
        return nil
    }
    func getBtn(_ text: String) -> UIButton {
        let btn = XS_NoHighlightBtn()
        btn.setTitle(text, for: .normal)
        btn.setTitleColor(#colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), for: .disabled)
        btn.setTitleColor(#colorLiteral(red: 0.5333333333, green: 0.5568627451, blue: 0.5764705882, alpha: 1), for: .normal)
        btn.titleLabel?.font = .xsFont(name: .medium, .scale*16)
        return btn
    }
}
